<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Rules;

use App\Enums\HouseLeaseModeEnum;
use App\Enums\HouseLeaseStatusEnum;
use App\Enums\HousePriceRangeEnum;
use App\Enums\HouseSpecsEnum;
use App\Enums\HouseTradeModeEnum;
use Illuminate\Contracts\Validation\Rule;

class CheckHouse implements Rule
{
    private $message;

    /**
     * Create a new rule instance.
     */
    public function __construct()
    {
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param string $attribute
     * @param mixed $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        if (! is_array($value)) {
            $this->message = '房屋数据格式异常';
            return false;
        }
        // 如果为空表示不创建或者清空全部
        if (empty($value)) {
            return true;
        }
        foreach ($value as $house) {
            if (! $this->checkOne($house)) {
                return false;
            }
        }
        return true;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return $this->message;
    }

    private function checkOne($house)
    {
        $mustColumns = [
            'address' => (function ($house) {
                if (strlen($house['address']) > 100) {
                    $this->message = 'address 字段过长';
                    return false;
                }
                return true;
            }),
            'size' => (function ($house) {
                if (strlen($house['size']) < 1) {
                    $this->message = 'size 字段为空';
                    return false;
                }
                return true;
            }),
            'lease_mode' => (function ($house) {
                if (! in_array($house['lease_mode'], array_keys(HouseLeaseModeEnum::toArray()))) {
                    $this->message = 'lease_mode 不合法';
                    return false;
                }
                return true;
            }),
            'specs' => (function ($house) {
                if (! in_array($house['specs'], array_keys(HouseSpecsEnum::toArray()))) {
                    $this->message = 'specs 不合法';
                    return false;
                }
                return true;
            }),
            'trade_mode' => (function ($house) {
                if (! in_array($house['trade_mode'], array_keys(HouseTradeModeEnum::toArray()))) {
                    $this->message = 'trade_mode 不合法';
                    return false;
                }
                return true;
            }),
            'price_range' => (function ($house) {
                if (! in_array($house['price_range'], array_keys(HousePriceRangeEnum::toArray()))) {
                    $this->message = 'price_range 不合法';
                    return false;
                }
                return true;
            }),
            'lease_status' => (function ($house) {
                if (! in_array($house['lease_status'], array_keys(HouseLeaseStatusEnum::toArray()))) {
                    $this->message = 'lease_status 不合法';
                    return false;
                }
                return true;
            }),
            // 'remarks' => (function ($house) {
            //    if (strlen($house['address']) > 100) {
            //        $this->message = 'remarks 字段过长';
            //        return false;
            //    }
            //    return true;
            // }),
            'images' => (function ($house) {
                if (strlen($house['images']) < 1) {
                    $this->message = 'images 字段不合法';
                    return false;
                }
                return true;
            }),
            'houser_user_id' => (function ($house) {
                return true;
            }),
            //'tenants_id_card_num' => (function ($house) {
            //    return true;
            //}),
        ];
        foreach ($mustColumns as $column => $closure) {
            if (! isset($house[$column])) {
                $this->message = '缺少 ' . $column . ' 字段';
                return false;
            }
            if (isset($house['remarks']) && strlen($house['address']) > 100) {
                $this->message = 'remarks 字段过长';
            }
            if ($closure($house) === false) {
                return false;
            }
        }
        return true;
    }
}
